using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;

namespace LowLevelGraphics.ImageAcquiring
{
    public enum FlickrSearchType
    {
        Text,
        Tags,
        None,
        All
    }

    /// <summary>
    /// 
    /// </summary>
    public class FlickrOnlineSearchProvider : OnlineSearchProvider
    {
        public event EventHandler FileDownloaded = null;

        public Dictionary<FlickrSearchType, string> m_aSearchType = new Dictionary<FlickrSearchType, string>();

        private FlickrSearchType m_SearchType = FlickrSearchType.None;

        /// <summary>
        /// Initializes a new instance of the <see cref="FlickrOnlineSearchProvider"/> class.
        /// </summary>
        public FlickrOnlineSearchProvider()
        {
            this.Host = "flickr.com";

            m_aSearchType.Add(FlickrSearchType.Tags, "tags");   //gross
            m_aSearchType.Add(FlickrSearchType.Text, "text"); //extragross
            m_SearchType = FlickrSearchType.Text;
        }

        /// <summary>
        /// Gets the flickr string.
        /// </summary>
        /// <param name="_sSearch">The _s search.</param>
        /// <returns></returns>
        public string GetFlickrString(string _sSearch)
        {
            string[] aElem = _sSearch.Split(' ');
            return string.Join("+", aElem);
        }

        /// <summary>
        /// Downloads the files.
        /// </summary>
        /// <param name="_sSearch">The _s search.</param>
        public override void DownloadFiles(object _sSearch)
        {
            DownloadFlickrPics((string)_sSearch, true);
        }

        /// <summary>
        /// Downloads the flickr pics.
        /// </summary>
        /// <param name="_sSearch">The _s search.</param>
        /// <param name="_bUseWebClient">if set to <c>true</c> [_b use web client].</param>
        protected void DownloadFlickrPics(string _sSearch, bool _bUseWebClient)
        {
            string sText = string.Empty;

            if (_bUseWebClient)
            {
                WebClient client = new WebClient();
                string sDownloadString = "http://" + this.Host + "/search/?q=" + _sSearch + "&m=" + m_aSearchType[m_SearchType];
                sText = client.DownloadString(sDownloadString);
            }
            else
            {
                sText = GetSocketData(this.Host, "/search/?q" + _sSearch, this.Host);
            }

            string[] saLink = Regex.Split(sText, "/photos/");
            for (int i = 1; i < saLink.Length; i++)
            {
                try
                {
                    string sLink = saLink[i];
                    if (sLink.Contains("/buddyicons/")) continue;

                    string sPic = string.Empty;
                    if (sLink.Contains(".jpg"))
                    {
                        string[] saPicTemp = Regex.Split(sLink, "img src");
                        string[] saPicTemp2 = Regex.Split(saPicTemp[1], ".jpg");
                        sPic = saPicTemp2[0] + ".jpg";
                        sPic = sPic.Replace("=", "").Replace("\"", "");
                    }
                    Console.WriteLine("Pic " + i.ToString() + ": " + sPic);
                    HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(sPic);
                    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

                    Stream stream = webResponse.GetResponseStream();
                    if (FileDownloaded != null)
                    {
                        FileDownloaded(stream, new EventArgs());
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return "Flickr";
        }
    }
}
